<?php

namespace App\Http\Controllers\service;

use App\Models\Sys;
use EasyWeChat\Factory;

class XcxService
{
    private $config;
    public function __construct()
    {
        $this->config=[
            'app_id'=>Sys::getValue('wx_appid'),
            'secret'=>Sys::getValue('wx_secret'),
            'response_type'=>'array',
            'log' => [
                'level' => 'debug',
                'file' => __DIR__.'/wechat.log',
            ],
        ];
    }

    public function login_xcx()
    {
        $app = Factory::miniProgram($this->config);
        return $app;
    }
    
    public function crate_xcx_qrcode($data,$path="/pages/index/index")
    {
        $data=$path."?".$data;
       $app = Factory::miniProgram($this->config);
       $response = $app->app_code ->get($data,[
           'page'=>$path
           ]);
           $img_url=base_path()."/public/xcx_qrcode/";
           $file_name=md5(time().$data).".png";
       if($response instanceof \EasyWeChat\Kernel\Http\StreamResponse ){
           $qr_name=$response->saveAs($img_url,$file_name);
           return ['path'=>"/xcx_qrcode/".$qr_name];
       }
       return ['path'=>null];

    }

}
